package com.gdssoft.system;

import java.util.Date;

import javax.annotation.Resource;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;
import com.gdssoft.BaseTestCase;
import com.gdssoft.core.Constants;
import com.gdssoft.core.exception.ExistException;
import com.gdssoft.core.exception.NotCompleteException;
import com.gdssoft.core.exception.NotExistException;
import com.gdssoft.core.model.CheckCompleteResult;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.model.system.Department;
import com.gdssoft.framework.model.system.Employee;
import com.gdssoft.framework.service.system.AppUserService;
import com.gdssoft.framework.service.system.DepartmentService;
import com.gdssoft.framework.service.system.EmployeeService;

public class AppUserServiceTestCase extends BaseTestCase {
	@Resource
	private AppUserService appUserService;
	@Resource
	private EmployeeService employeeService;
	@Resource
	private DepartmentService departmentService;
	
	private Employee defaultEmp;

	@Before
	public void beforeTest() {
		defaultEmp = employeeService.get(new Long(-1));
	}
	
	@Test
	@Rollback(true)
	public void testAppUserService() throws Exception {
		AppUser entity = new AppUser();
		
		//测试检查完整性
		CheckCompleteResult result = entity.checkComplete();
		//必须有检查完整性的结果对象
		TestCase.assertNotNull("check result should't Null", result);
		//因信息不完整，检查结果为假
		TestCase.assertFalse("check result should be False", result.isComplete());
		
		//填充必要信息
		entity.setUsername("admin");
		entity.setPassword("pp");
		entity.setEmployee(defaultEmp);
		entity.setStatus(Constants.ENABLED);
		entity.setDelFlag(Constants.FLAG_UNDELETED);

		//再次检查完整性
		CheckCompleteResult result2 = entity.checkComplete();
		//检查结果应为真
		TestCase.assertTrue("check result should be True", result2.isComplete());
		
		testSave(entity);
		
		//利用getByUserName方法重新获取员工对象
		AppUser appUser = appUserService.getByUserName("_test_");
		//结果不为Null
		TestCase.assertNotNull("appUser should't Null", appUser);
		//对应的员工不为Null
		TestCase.assertNotNull("employee should't Null", appUser.getEmployee());
		//员工名称为系统默认用户
		TestCase.assertEquals("Fullname should be 系统用户", "系统用户", appUser.getFullname());

		//测试更新
		appUser = testUpdate(appUser);
		
		//测试移除
		testRemove(appUser);
	}
	
	private void testSave(AppUser entity) {
		//新增
		try {
			appUserService.save(entity);
			TestCase.fail("Should not be successful");
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			//expected 因账号已存在，新增员工失败
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		} 

		//修改成一个未使用的账号，再次新增
		AppUser appUser = null;
		entity.setUsername("_test_");
		try {
			appUser = appUserService.save(entity);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		} 
		//新增成功，返回结果不为Null
		TestCase.assertNotNull("appUser should't Null", appUser);
	}
	
	private AppUser testUpdate(AppUser entity) {
		//测试更新
		entity.setStatus(Constants.UNENABLED);
		AppUser appUser = null;
		try {
			appUser = appUserService.save(entity);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//返回结果不为Null
		TestCase.assertNotNull("appUser should't Null", appUser);
		//更新前后对象ID相等
		TestCase.assertTrue("should be True", appUser.getUserId().equals(entity.getUserId()));
		//状态应该已经变成失效了
		TestCase.assertTrue("should be True", appUser.getStatus().equals(Constants.UNENABLED));
		
		return appUser;
	}
	
	private void testRemove(AppUser entity) {
		//测试移除
		Long userId = entity.getUserId();
		try {
			appUserService.remove(userId);
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//利用get方法重新获取账号对象
		AppUser appUser = appUserService.get(userId);
		//返回结果为Null
		TestCase.assertTrue("should be True", appUser.getDelFlag()==1L);
	}
	
	
	/*
	@Test
	@Rollback(false)
	public void testSaveAppUser(){
		Department department = departmentService.get(new Long(46));
		Employee employee = new Employee();
		//填充必要员工信息
		employee.setEmpCode("F3201253");
		employee.setFullname("许健");
		employee.setDepartment(department);
		employee.setBirthday(new Date("1984/05/07"));
		employee.setSex((short)1);
		employee.setAccessionDate(new Date("2005/03/25"));
		
		AppUser appUser = new AppUser();

		//填充必要信息
		appUser.setUsername("admin11");
		appUser.setPassword("ppp");
		appUser.setEmployee(employee);
		appUser.setStatus(Constants.ENABLED);
		appUser.setDelFlag(Constants.FLAG_UNDELETED);
		
		try {
			appUserService.save(appUser);
		} catch (NotCompleteException e) {
			e.printStackTrace();
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			e.printStackTrace();
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			e.printStackTrace();
			TestCase.fail("Should not be an exception");
		}
	}
	*/
}